<?xml version="1.0" encoding="UTF-8"?>
<!--
   - list2antxqy.xsl
   - Build utility for preprocessing tag documents to XQuery
   -
   - Copyright (c) 2011 Erik Hennum. All Rights Reserved.
   -
   - Licensed under the Apache License, Version 2.0 (the "License");
   - you may not use this file except in compliance with the License.
   - You may obtain a copy of the License at
   -
   - http://www.apache.org/licenses/LICENSE-2.0
   -
   - Unless required by applicable law or agreed to in writing, software
   - distributed under the License is distributed on an "AS IS" BASIS,
   - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   - See the License for the specific language governing permissions and
   - limitations under the License.
   -
   - The use of the Apache License does not indicate that this work is
   - associated with the Apache Software Foundation.
   -
  -->
<xsl:stylesheet version="2.0"
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
    xmlns:fn  = "http://www.w3.org/2005/xpath-functions"
    xmlns:xs  = "http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xsl fn xs">

<xsl:param name="basedir" as="xs:string"/>

<xsl:variable name="problem-files" select="()"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="filelist">
  <xsl:text>
</xsl:text>
  <project name="xqy-test" default="xqy-all" basedir="{$basedir}">
    <target name="xqy-all">
      <xsl:comment>Preprocessing</xsl:comment>
      <xsl:apply-templates select="file" mode="preprocess"/>
      <xsl:comment>Running</xsl:comment>
      <mkdir>
        <xsl:attribute name="dir">
          <xsl:text>out${file.separator}xmlxqy</xsl:text>
        </xsl:attribute>
      </mkdir>
      <xsl:apply-templates select="file" mode="run"/>
    </target>
  </project>
</xsl:template>

<xsl:template match="file" mode="preprocess">
  <xsl:variable name="href" select="fn:string(@href)"/>
  <xsl:variable name="root" select="fn:replace(fn:replace($href,'\.xml$',''), '\.xstag$', '')"/>
  
  <echo>Preprocessing <xsl:value-of select="$href"/></echo>
  <java classname="net.sf.saxon.Query"
        failonerror="true">
    <arg>
      <xsl:attribute name="value">
        <!-- relative to user.dir (which for clarity is also basedir in this file)-->
        <xsl:text>-q:../xqy/doc2xq-separate.xq</xsl:text>
      </xsl:attribute>
    </arg>
    <arg>
      <xsl:attribute name="value">
        <!-- relative to user.dir -->
        <xsl:text>-o:out/</xsl:text>
        <xsl:value-of select="$root"/>
        <xsl:text>.xqy</xsl:text>
      </xsl:attribute>
    </arg>
    <arg>
      <xsl:attribute name="value">
        <!-- relative to query -->
        <xsl:text>INPUT=../test/out/</xsl:text>
        <xsl:value-of select="$href"/>
      </xsl:attribute>
    </arg>
  </java>
</xsl:template>

<xsl:template match="file" mode="run">
  <xsl:variable name="href" select="fn:string(@href)"/>
  <xsl:variable name="root" select="fn:replace($href,'\.xml$','')"/>
  
  <!-- skip files that are problematic at present -->
  <xsl:if test="fn:not($root = $problem-files) and fn:not(ends-with($href, '.xstag'))">
    <echo>Executing XQuery for <xsl:value-of select="$href"/></echo>
    
    <java classname="net.sf.saxon.Query"
          failonerror="true">
      <arg>
        <xsl:attribute name="value">
          <!-- relative to user.dir (which for clarity is also basedir in this file)-->
          <xsl:text>-q:out/</xsl:text>
          <xsl:value-of select="$root"/>
          <xsl:text>.xqy</xsl:text>
        </xsl:attribute>
      </arg>
      <arg>
        <xsl:attribute name="value">
          <!-- relative to user.dir -->
          <xsl:text>-o:out/xmlxqy/</xsl:text>
          <xsl:value-of select="$root"/>
          <xsl:text>.xml</xsl:text>
        </xsl:attribute>
      </arg>
    </java>
  </xsl:if>
</xsl:template>

<xsl:template match="node()">
  <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
